package com.xxl.job.admin.controller;

import com.xxl.job.admin.controller.annotation.PermissionLimit;
import com.xxl.job.admin.core.model.XxlJobGroup;
import com.xxl.job.admin.core.model.XxlJobUser;
import com.xxl.job.admin.core.thread.JobTriggerPoolHelper;
import com.xxl.job.admin.dao.XxlJobGroupDao;
import com.xxl.job.admin.dao.XxlJobUserDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Zhihj
 * @Date: 2023-10-12 21:19:23
 */

@Controller
@RequestMapping("/user")
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Resource
    private XxlJobGroupDao xxlJobGroupDao;
    @Resource
    private XxlJobUserDao xxlJobUserDao;


    @RequestMapping
    public String index(Model model){
        // 执行器列表
        List<XxlJobGroup> xxlJobGroupList = xxlJobGroupDao.findAll();
        model.addAttribute("groupList",xxlJobGroupList);
        logger.info("调用成功!");
        return "user/user.index";
    }
    /**
     * 根据姓名和用户角色分页查
     */
    @ResponseBody
    @RequestMapping("/pageList")
    @PermissionLimit(adminuser = true)//需要管理员权限才能查看
    public Map<String,Object>pageList(@RequestParam(required = false,defaultValue = "0") int start, @RequestParam(required = false,defaultValue = "10") int length,
                                      String username, int role){
        //查询出来对象
        ArrayList<XxlJobUser> list =xxlJobUserDao.pageList(start,length,username,role);
        int list_count = xxlJobUserDao.pageListCount(start,length,username,role);

        if(list != null && list.size() >0){
            for (XxlJobUser xxlJobUser : list) {
                xxlJobUser.setPassword(null);
            }
        }
        Map<String, Object> maps = new HashMap<>();
        maps.put("recordsTotal",list_count);
        maps.put("recordsFiltered", list_count);	// 过滤后的总记录数
        maps.put("data", list);
        return maps;
    }

}
